<?php
// 模型基类
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2017 http://www.eacoo123.com, All rights reserved.         
// +----------------------------------------------------------------------
// | [EacooPHP] 并不是自由软件,可免费使用,未经许可不能去掉EacooPHP相关版权。
// | 禁止在EacooPHP整体或任何部分基础上发展任何派生、修改或第三方版本用于重新分发
// +----------------------------------------------------------------------
// | Author:  心云间、凝听 <981248356@qq.com>
// +----------------------------------------------------------------------

namespace app\common\model;
use think\Model;

class BaseNew extends Model {

    protected $pk = 'id';
    protected $order = 'vieworder';
	
	function parseByKey($data, $new_key) {
		$list = array();
		!$new_key && $new_key = $this->getPk();
		foreach ($data as $key => $value) {
			$list[$value[$new_key]] = $value;
		}
		return $list;
	}
	
	//常规要用1.4(20210712)
	public function getInfo($id) {
		$info = $this->find($id); //实例化模型后调用查询方法
		return $info ? $this->parseInfo($info->toArray()) : []; //转换为数组并解析
	}

	public function baseAddData($data) {
		return $this->insertGetId($data); //新增数据并返回主键值
	}

	public function baseUpdateData($id, $data) {
		return $this->where(['id'=>$id])->update($data); //更新数据表中的数据
	}
	
	public function delInfo($id) {
		return $this->where(['id'=>$id])->delete(); //通过数据库类的查询条件删除
	}

	public function del($id) {
		return $this->where(['id'=>$id])->delete(); //通过数据库类的查询条件删除
	}
	
	//附加要用1.4(20210712)
	public function findMap($map) {
		$info = $this->where($map)->find();
		return $info ? $this->parseInfo($info->toArray()) : []; //转换为数组并解析
	}

	public function findLockInfo($map) {
		$info = $this->where($map)->lock(true)->find();
		return $info ? $this->parseInfo($info->toArray()) : []; //转换为数组并解析
	}

	public function getListByIds($ids) {
		$data = $this->where('id', 'IN', $ids)->select();
		$data = $data->toArray(); //转换为数组
		return $this->parseSearch($data);
	}

	public function deleteMap($map) {
		return $this->where($map)->delete();
	}
	
	public function deleteByIds($ids) {
		return $this->where('id', 'IN', $ids)->delete();
	}
	
	public function baseSumSearch($map, $field = 'total_fee') {
		return $this->where($map)->sum($field);
	}
	
}